<?php
/**
 * 点大商城（www.diandashop.com） - 微信公众号小程序商城系统!
 * Copyright © 2020 山东点大网络科技有限公司 保留所有权利
 * =========================================================
 * 版本：V2
 * 授权主体：shop.guanjunbang.cn
 * 授权域名：shop.guanjunbang.cn
 * 授权码：vdcajlVLIVAgVdkfJvgY
 * ----------------------------------------------
 * 您只能在商业授权范围内使用，不可二次转售、分发、分享、传播
 * 任何企业和个人不得对代码以任何目的任何形式的再发布
 * =========================================================
 */

namespace app\common;
use think\facade\Db;
class Business
{
	//加余额
	public static function addmoney($aid,$bid,$money,$remark,$addparentcommission=false){
		if($money==0) return ;
		$business = Db::name('business')->where('aid',$aid)->where('id',$bid)->find();
		if(!$business) return ['status'=>0,'msg'=>'商家不存在'];
		Db::name('business')->where('aid',$aid)->where('id',$bid)->inc('money',$money)->update();
		
		$data = [];
		$data['aid'] = $aid;
		$data['bid'] = $bid;
		$data['money'] = $money;
		$data['after'] = $business['money'] + $money;
		$data['createtime'] = time();
		$data['remark'] = $remark;
		Db::name('business_moneylog')->insert($data);

		if($addparentcommission && $money > 0){
			if(getcustom('business_agent')){
				self::addparentcommission2($aid,$bid,$money);
			}else{
				self::addparentcommission($aid,$bid,$money);
			}
		}
		return ['status'=>1,'msg'=>''];
	}
	public static function addparentcommission($aid,$bid,$money){
		$bset = Db::name('business_sysset')->where('aid',$aid)->find();
		if($bset['parentcommission'] > 0){
			$business = Db::name('business')->where('id',$bid)->find();
			$buser = Db::name('admin_user')->where('aid',$aid)->where('bid',$bid)->where('isadmin',1)->find();
			if($buser && $buser['mid']){
				$member = Db::name('member')->where('id',$buser['mid'])->find();
				if($member && $member['pid']){
					$commission = round($money * $bset['parentcommission'] * 0.01,2);
					if($commission > 0){
						if(getcustom('shoptongji3')){ //商家推荐商家提成
							$isadminuser = Db::name('admin_user')->where('aid',$aid)->where('bid','>',0)->where('mid',$member['pid'])->where('isadmin',1)->find();
							if($isadminuser && $isadminuser['bid'] != $bid){
								\app\common\Business::addmoney($aid,$isadminuser['bid'],$commission,'商户['.$business['name'].']营业提成');
							}
						}else{
							\app\common\Member::addcommission($aid,$member['pid'],$member['id'],$commission,'商户['.$business['name'].']营业提成');
						}
					}
				}
			}
		}
	}
	public static function addparentcommission2($aid,$bid,$money){

		if(getcustom('business_agent')){
			$business = Db::name('business')->where('id',$bid)->find();
			$buser    = Db::name('admin_user')->where('aid',$aid)->where('bid',$bid)->where('isadmin',1)->find();

			if($buser && $buser['mid']){
				$member = Db::name('member')->where('id',$buser['mid'])->field('id,pid,levelid')->find();
				\think\facade\Log::write(json_encode($member));
				if($member && $member['pid']){
					//查询上级信息
					$parent = Db::name('member')->where('id',$member['pid'])->where('aid',$aid)->field('id,pid,levelid')->find();
					\think\facade\Log::write(json_encode($parent));
					if($parent){

						//查询上级等级信息
						$plevel = Db::name('member_level')->where('id',$parent['levelid'])->where('can_agent','>',0)->where('aid',$aid)->field('business_zt_ratio')->find();
						\think\facade\Log::write(json_encode($plevel));
						if($plevel && $plevel['business_zt_ratio']>0){
							//发直推商家分成
							$parentcommission = round($money * $plevel['business_zt_ratio'] * 0.01,2);
							\think\facade\Log::write(json_encode($parentcommission));
							if($parentcommission > 0){
								\app\common\Member::addcommission($aid,$member['pid'],$member['id'],$parentcommission,'商户['.$business['name'].']营业提成');
							}
						}

						if($parent['pid']>0){
							//查询上上级信息
							$parent2 = Db::name('member')->where('id',$parent['pid'])->where('aid',$aid)->field('id,levelid')->find();
							if($parent2){
								//查询上上级等级信息
								$plevel2 = Db::name('member_level')->where('id',$parent2['levelid'])->where('can_agent','>',0)->where('aid',$aid)->field('business_jt_ratio')->find();
								if($plevel2 && $plevel2['business_jt_ratio']>0){
									//发间推商家分成
									$parent2commission = round($money * $plevel2['business_jt_ratio'] * 0.01,2);
									if($parent2commission > 0){
										\app\common\Member::addcommission($aid,$parent['pid'],$member['id'],$parent2commission,'商户['.$business['name'].']营业间推提成');
									}
								}
							}
						}
					}
				}
			}
		}
	}
}